package com.epfresh.supplier.activity;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.utils.T;
import com.epfresh.supplier.R;
import es.voghdev.pdfviewpager.library.RemotePDFViewPager;
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter;
import es.voghdev.pdfviewpager.library.remote.DownloadFile;
import es.voghdev.pdfviewpager.library.util.FileUtil;

public class WebPDFActivity extends BaseActivity implements DownloadFile.Listener {
    private ProgressBar pb_bar;
    private RemotePDFViewPager remotePDFViewPager;
    private PDFPagerAdapter adapter;
    private LinearLayout remote_pdf_root;
    private TextView textView;
    private String pdfUrl, titleName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webpdf);
        initData();
        initView();
        setDownloadListener();
    }

    private void initView() {
        pb_bar = findViewById(R.id.pb_bar);
        textView = findViewById(R.id.tv_page);
        remote_pdf_root = findViewById(R.id.remote_pdf_root);
        setToolbarTitle(titleName);
    }

    private void initData() {
        pdfUrl = getIntent().getStringExtra("pdfUrl");
        titleName = getIntent().getStringExtra("titleName");
    }

    /*设置监听*/
    protected void setDownloadListener() {
        try {
            final DownloadFile.Listener listener = this;
            remotePDFViewPager = new RemotePDFViewPager(this, pdfUrl, listener);
            remotePDFViewPager.setId(R.id.pdfViewPager);
            remotePDFViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    int page = position + 1;
                    textView.setText("当前是第" + page + "页，共" + adapter.getCount() + "页");
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        } catch (Exception e) {
            pb_bar.setVisibility(View.GONE);
            T.toast("数据加载错误");
        }
    }

    /*更新视图*/
    private void updateLayout() {
        remote_pdf_root.removeAllViewsInLayout();
        remote_pdf_root.addView(remotePDFViewPager, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        textView.setText("当前是第1" + "页，共" + adapter.getCount() + "页");
    }


    @Override
    public void onSuccess(String url, String destinationPath) {
        pb_bar.setVisibility(View.GONE);
        adapter = new PDFPagerAdapter(this, FileUtil.extractFileNameFromURL(url));
        remotePDFViewPager.setAdapter(adapter);
        updateLayout();
    }

    @Override
    public void onFailure(Exception e) {
        pb_bar.setVisibility(View.GONE);
        T.toast("数据加载错误");
    }

    @Override
    public void onProgressUpdate(int progress, int total) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter.close();
    }
}
